找传奇、传世资源到传世资源站!

windows网络编程_文件传输

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

  简单的tcp/ip协议服务端和客户端连接,文件传输windows网络编程_文件传输 C/C++网络编程-第1张
from clipboard
int gConnectSocket_TCP(SOCKET &_socket, char *_ipAddr, int _port, int _bufsize/*=100*1024*/, int timeout/* = 5000*/){int err;//创建Socket句柄_socket = socket(AF_INET, SOCK_STREAM, 0);if (INVALID_SOCKET == _socket){//printf("socket failed with error: %ld\n", WSAGetLastError());WSACleanup();return 1;}//设置服务器IP地址SOCKADDR_IN addrSrv;addrSrv.sin_addr.S_un.S_addr = inet_addr(_ipAddr);addrSrv.sin_family = AF_INET;addrSrv.sin_port = htons(_port);//resize recv bufferif (::setsockopt(_socket, SOL_SOCKET, SO_RCVBUF, (char*)&_bufsize, sizeof(_bufsize))){goto error;}int StreamTimeOut = timeout;// set receive timeout if (::setsockopt(_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&StreamTimeOut, sizeof(StreamTimeOut)) == SOCKET_ERROR){goto error;}//set non-blocking modeunsigned long ul = 1;err = ioctlsocket(_socket, FIONBIO, (unsigned long*)&ul);if (err == SOCKET_ERROR){goto error;}//connect to cameraint ret = connect(_socket, (const struct sockaddr *)&addrSrv, sizeof(addrSrv));//TRACE(L"videoSocket connect returnVal=%d",ret);//use select poll connection statusstruct timeval ConnectTimeOut;fd_set w;FD_ZERO(&w);FD_SET(_socket, &w);ConnectTimeOut.tv_sec = timeout / 1000;ConnectTimeOut.tv_usec = 0;err = select(0, 0, &w, 0, &ConnectTimeOut);if (err <= 0){goto error;}//resume blocking modeunsigned long mode = 0;err = ioctlsocket(_socket, FIONBIO, (unsigned long*)&mode);if (err == SOCKET_ERROR){goto error;}return 0;error:return -2;}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复